package foo.bar.forum.radeox;

import foo.bar.radeox.engine.BaseRenderEngine;
import foo.bar.radeox.api.engine.WikiRenderEngine;
import foo.bar.radeox.api.engine.IncludeRenderEngine;
import foo.bar.radeox.api.engine.ImageRenderEngine;
import foo.bar.radeox.api.engine.context.InitialRenderContext;
import foo.bar.forum.services.ForumService;
import foo.bar.forum.web.action.general.ExternalLinkImageActionBean;
import foo.bar.forum.web.action.general.MailToImageActionBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sourceforge.stripes.util.UrlBuilder;

import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: tmjee
 * Date: Jun 19, 2009
 * Time: 10:41:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class DefaultForumRenderEngine extends BaseRenderEngine implements
        IncludeRenderEngine, ImageRenderEngine {

    private static final Log LOG = LogFactory.getLog(DefaultForumRenderEngine.class);

    public static final String RENDER_CONTEXT_SPACE_ID_PARAM_KEY = "RENDER_CONTEXT_SPACE_ID_PARAM_KEY";


    private ForumService forumService;

    public DefaultForumRenderEngine(ForumService forumService, InitialRenderContext initialRenderContext) {
        super(initialRenderContext);
        this.forumService = forumService;
    }


    // === IncludeRenderEngine ========================================

    public String include(String s) {
        return null;
    }

    public String includeResource(String name) {
        /*
        String spaceId = getSpaceId();
        Resource resource = forumService.getResourceByNameAndSpaceId(name, spaceId);
        UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), ResourceRetrievalActionBean.class, true);
        urlBuilder.setEvent("view");
        urlBuilder.addParameter("space", spaceId);
        urlBuilder.addParameter("resourceName", name);

        if (resource.getMimeType().toLowerCase().startsWith("image")) {
            return "<img src='"+urlBuilder.toString()+"'/>";
        }
        else {
            return "<a href='"+urlBuilder.toString()+"'>"+name+"</a>";
        }
        */
        return null;
    }


    // === ImageRenderEngine interface ====================================================

    public String getExternalImageLink() {
        UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), ExternalLinkImageActionBean.class, true);
        urlBuilder.setEvent("view");
        return "<img src='"+urlBuilder.toString()+"'/>";
    }

    public String getMailToImageLink() {
        UrlBuilder urlBuilder = new UrlBuilder(Locale.getDefault(), MailToImageActionBean.class, true);
        urlBuilder.setEvent("view") ;
        return "<img src='"+urlBuilder.toString()+"'/>";
    }


    // ========= commons ===========
    protected String getSpaceId() {
        String spaceId = (String) renderContext.getParameters().get(RENDER_CONTEXT_SPACE_ID_PARAM_KEY);
        if ((spaceId == null) || (spaceId.trim().length() <= 0)) {
            throw new IllegalArgumentException("Cannot find space Id from RenderEngine ["+this+"]'s RenderContext ["+renderContext+"] with key ["+RENDER_CONTEXT_SPACE_ID_PARAM_KEY+"]");
        }
        return spaceId;
    }
}

